home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
IRIX 6.5 Applications 1999 May
/
SGI IRIX 6.5 Applications 1999 May.iso
/
dist
/
sitemgr.idb
/
usr
/
sitemgr
/
bin
/
html2ps.z
/
html2ps
Wrap
Text File
|
1998-05-04
|
33KB
|
1,206 lines
#!/usr/bin/perl
#
# This is html2ps 0.2 alpha PL5, an HTML-to-PostScript converter.
# Copyright (C) 1995 Jan Karrman.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Author: Jan Karrman, Dept. of Scientific Computing, Uppsala University,
# Sweden, e-mail: jan@tdb.uu.se.
#
# 97Apr04 Greg Ferguson/gferg@sgi.com: added sgi extensions
#
# Set the following three variables to 1 or 0 if you have or have not the
# corresponding package installed:
$have_ImageMagick=0;
$have_pbmplus=0;
$have_netpbm=0;
$have_djpeg=0;
$have_sgi=1;
# You may have to change the names of the temporary directory and filename
# if you are not on a UNIX system:
$tmpdir="/tmp/";
$tmpnam="h2p$$";
# No editing necessary below this line
$usage=<<EOU;
Usage:
html2ps [-2aCcFgLnOTUuvX] [-B 0|1|2] [-b URL] [-cw program] [-f font]
[-hn scalefactor] [-i scalefactor] [-IM text] [-l length] [-LM size]
[-m scalefactor] [-MM size] [-N num] [-o file] [-p scalefactor] [-PS size]
[-s scalefactor] [-tn style] [-TM size] [-w width] [-x 0|1|2] [-zl text]
[-zr text] [URL|file]
EOU
$version="html2ps 0.2 alpha";
$styles="Possible choices are: n, i, b, bi, f, fb, fi and fbi";
$opts="2|a|c|C|F|g|L|n|O|T|u|U|v|X|B:|b:|s:|w:|l:|m:|p:|f:|o:|cw:|i:|IM:|PS:"
."|LM:|TM:|MM:|h1:|h2:|h3:|h4:|h5:|h6:|t1:|t2:|t3:|t4:|t5:|t6:|x:|zl:|zr:|N:";
$ulanch="t";
$number="f";
$mask=7;
$opt_PS=29.7;
$opt_LM=2.5;
$opt_TM=3;
$opt_MM=2;
$opt_s=1;
$opt_p=1;
$opt_i=1;
$opt_m=1;
$opt_f="n";
$opt_f="n";
$opt_B=0;
$opt_h1=1;
$opt_h2=1;
$opt_h3=1;
$opt_h4=1;
$opt_h5=1;
$opt_h6=1;
$opt_t1="b";
$opt_t2="b";
$opt_t3="b";
$opt_t4="b";
$opt_t5="b";
$opt_t6="b";
$ff="Courier";
$fb="Courier-Bold";
$fi="Courier-Oblique";
$ft="Courier-BoldOblique";
$opt_IM="[IMAGE]";
$opt_N=1;
$d=9;
$f=72/2.54;
$geturl="lynx -source";
$geturl="/usr/sitemgr/bin/smgeturl" if($have_sgi);
$giftopm="giftopnm" if($have_netpbm);
$giftopm="giftoppm" if($have_pbmplus);
$giftopm="fromgif" if($have_sgi);
$scr=$tmpdir.$tmpnam;
&Getopts($opts) || die $usage;
die $usage if $opt_U;
print STDERR "$version\n" if $opt_v;
if ($opt_a) {$mask=0};
if ($opt_n) {$number="t"};
if ($opt_u) {$ulanch="f"};
if ($opt_PS=~/^a4$/i) {$opt_PS=29.7};
if ($opt_PS=~/^us$/i) {$opt_PS=27.9};
if($d*$opt_s>15) {$d=int(15/$opt_s)};
$twoup=$opt_2?"t":"f";
$html=$ARGV[0];
$lfoot="f";
$rfoot="f";
if ($opt_zl) {
$lfoot="t";
$opt_zl=~s/\$URL/$html/g;
}
if ($opt_zr) {
$rfoot="t";
$opt_zr=~s/\$URL/$html/g;
}
if($opt_B!~/^[0-2]$/) {die "Illegal value for -B: $opt_B\n"}
if($set_x && $opt_x!~/^[0-2]$/) {die "Illegal value for -x: $opt_x\n"}
$F='-?\d*\.?\d*';
for $o
($opt_s,$opt_w,$opt_l,$opt_p,$opt_LM,$opt_TM,$opt_MM,$opt_PS,$opt_i,$opt_m,
$opt_N,$opt_h1,$opt_h2,$opt_h3,$opt_h4,$opt_h5,$opt_h6)
{die "Non numeric: $o\n" if($o!~/^$F$/)}
if($opt_2) {
$opt_L=1;
$opt_s=0.65*$opt_s;
}
$opt_N=int($opt_N);
$opt_N--;
$mm=int($opt_MM*$f);
$is=0.8*$opt_i;
$msc=1/$opt_s;
$mag=1000*$opt_m*$opt_s;
$h1=19*$opt_h1;
$h2=17*$opt_h2;
$h3=15*$opt_h3;
$h4=14*$opt_h4;
$h5=13*$opt_h5;
$h6=12*$opt_h6;
if ($opt_L) {
$opt_w=$opt_2?11.5:24.5 unless $opt_w;
$opt_l=15 unless $opt_l;
$xc=int($opt_TM*$f);
$yc=int($opt_LM*$f);
$w=int($opt_w*$f);
$l=int($opt_l*$f);
$rot=" 90 rotate"
}
else {
$opt_w=16 unless $opt_w;
$opt_l=23 unless $opt_l;
$xc=int($opt_LM*$f);
$yc=int(($opt_PS-$opt_TM)*$f);
$w=int($opt_w*$f);
$l=int($opt_l*$f)
}
if ($opt_F) {
$xr=int($w/$opt_s)+$d;
$yb=-int($l/$opt_s)-$d;
$fr="\n gsave 1 setlinejoin -$d $d M $xr $d L $xr $yb L -$d $yb L -$d $d L"
."\n closepath stroke grestore"
}
if($opt_x==0 && defined $opt_x) {
$dupl="statusdict /setduplexmode known\n"
." {statusdict begin false setduplexmode end} if"
}
if($opt_x==1) {
$dupl="statusdict /setduplexmode known\n"
." {statusdict begin true setduplexmode end} if"
}
if($opt_x==2) {
$dupl="statusdict /settumble known\n"
." {statusdict begin true setduplexmode true settumble end} if"
}
@n=(
"NewCenturySchlbk-Roman",
"NewCenturySchlbk-Bold",
"NewCenturySchlbk-Italic",
"NewCenturySchlbk-BoldItalic");
@t=(
"Times-Roman",
"Times-Bold",
"Times-Italic",
"Times-BoldItalic");
@h=(
"Helvetica",
"Helvetica-Bold",
"Helvetica-Oblique",
"Helvetica-BoldOblique");
@hn=(
"Helvetica-Narrow",
"Helvetica-Narrow-Bold",
"Helvetica-Narrow-Oblique",
"Helvetica-Narrow-BoldOblique");
@p=(
"Palatino-Roman",
"Palatino-Bold",
"Palatino-Italic",
"Palatino-BoldItalic");
@a=(
"AvantGarde-Book",
"AvantGarde-Demi",
"AvantGarde-BookOblique",
"AvantGarde-DemiOblique");
@b=(
"Bookman-Light",
"Bookman-Demi",
"Bookman-LightItalic",
"Bookman-DemiItalic");
# Below is an example how to add a new font, specified on the command line
# with "-f myf". Remove the "#" characters and change the names into the
# appropriate for your font. Be sure that you get the names of the different
# styles right. You should also add the name ("myf" in this example) to the
# list of fonts on the line "$pfonts="Possible choices..." below the example.
#@myf=(
# "MyFont",
# "MyFont-Bold",
# "MyFont-Italic",
# "MyFont-BoldItalic");
$pfonts="Possible choices are: n, t, h, hn, p, a and b";
$nf=eval "\$$opt_f\[0]" || die "Unknown font: $opt_f\n$pfonts\n";
$bf=eval "\$$opt_f\[1]";
$if=eval "\$$opt_f\[2]";
$bi=eval "\$$opt_f\[3]";
%style=("n",1, "f",2, "b",3, "fb",4, "i",5, "fi",6, "bi",7, "fbi",8);
for $i (1..6) {
$temp=eval "\$opt_t$i";
die "Unknown font style: $temp\n$styles\n" if !$style{$temp};
eval "\$t$i=$style{$temp}-1"
}
%latin1=(
"AElig",306,
"Aacute",301,
"Acirc",302,
"Agrave",300,
"Aring",305,
"Atilde",303,
"Auml",304,
"Ccedil",307,
"ETH",320,
"Eacute",311,
"Ecirc",312,
"Egrave",310,
"Euml",313,
"Iacute",315,
"Icirc",316,
"Igrave",314,
"Iuml",317,
"Ntilde",321,
"Oacute",323,
"Ocirc",324,
"Ograve",322,
"Oslash",330,
"Otilde",325,
"Ouml",326,
"THORN",336,
"Uacute",332,
"Ucirc",333,
"Ugrave",331,
"Uuml",334,
"Yacute",335,
"aacute",341,
"acirc",342,
"aelig",346,
"agrave",340,
"aring",345,
"atilde",343,
"auml",344,
"ccedil",347,
"eacute",351,
"ecirc",352,
"egrave",350,
"eth",360,
"euml",353,
"iacute",355,
"icirc",356,
"igrave",354,
"iuml",357,
"ntilde",361,
"oacute",363,
"ocirc",364,
"ograve",362,
"oslash",370,
"otilde",365,
"ouml",366,
"szlig",337,
"thorn",376,
"uacute",372,
"ucirc",373,
"ugrave",371,
"uuml",374,
"yacute",375,
"yuml",377,
"copy",251,
"reg",256,
"nbsp",240);
undef $/;
if($html=~m|://| || $html=~/^news:/) {
&gethtml;
unless(($ba2)=$html=~m|(.*://.*/)|) {$ba2=$html."/"}
} elsif ($html) {
open(FILE,$html);
$_=<FILE>;
close FILE;
} else {
$_=<>
}
s|\r||g;
for ($opt_IM,$opt_zl,$opt_zr) {
s|\\|\\200|g;
s|\(|\\201|g;
s|\)|\\202|g;
}
$P1=<<EOC;
%!
%%Title: @ARGV
%%DocumentFonts: $nf $bf $if $bi $ff $fb $fi $ft
%%Creator: $version, written by Jan Karrman, jan\@tdb.uu.se
%%EndComments
save
1000 dict begin
/D {def} def
/t {true} D
/f {false} D
/RF {/$nf} D
/BF {/$bf} D
/IF {/$if} D
/IB {/$bi} D
/FF {/$ff} D
/FB {/$fb} D
/FI {/$fi} D
/FT {/$ft} D
/F $opt_s D
/W {$w F div} D
/LE {$l F div} D
/PS $opt_p D
/HS [$h1 $h2 $h3 $h4 $h5 $h6] D
/HT [$t1 $t2 $t3 $t4 $t5 $t6] D
/MK $mask D
/NO $number D
/ZL $lfoot D
/ZR $rfoot D
/Zl ($opt_zl) D
/Zr ($opt_zr) D
/TU $twoup D
/AU $ulanch D
/SN $opt_N D
/LA {TU PM 1 eq and {W $mm F div add 0 translate}
{$xc $yc translate$rot F dup scale} ie$fr} D
/FP $opt_B D
/SG [$is $opt_i $msc] D
$dupl
%-- End of variable part --
EOC
if(/^%!/ && /%-- End of variable part --/) {
&openps if ($opt_o);
print $P1.$';
for $s ("b","C","c","cw","g","t") {
print STDERR "Option -$s ignored\n" if(eval "\$opt_$s");
}
exit
}
if($opt_c || $opt_C || $opt_cw) {
$opt_cw="weblint" unless $opt_cw;
$file=$html;
if($html=~m|://|) {
open(SCRATCH,">$scr");
print SCRATCH;
close SCRATCH;
$file="$scr";
}
print STDERR `$opt_cw $file`;
exit if($opt_C);
}
for $tag ("head","title") {
if(m|</$tag>|i) {
$head=$`;
$body=$';
if($head=~/<plaintext|<xmp|<listing/i) {
$head="";
$body=$_
}
$_=$body
}
}
$b2=$opt_b;
unless($b2) {
($b2)=$head=~/<base[ \n]+href *= *"([^"]*)"[^>]*>/i;
unless($b2) {($b2)=$head=~/<base[ \n]+href *= *([\w\.-]+)[^>]*>/i}
unless($b2) {$b2=$ba2}
}
$b2=~s|[^/]*$||;
($b1)=$b2=~m|(.*://[^/]*)/|;
$P1.=<<EOT;
/ie {ifelse} D
/E {exch} D
/M {moveto} D
/S {show} D
/R {rmoveto} D
/L {lineto} D
/RL {rlineto} D
/CP {currentpoint} D
/SW {stringwidth} D
/GI {getinterval} D
/PI {putinterval} D
/U {gsave CP currentfont /FontInfo get /UnderlinePosition get
0 E currentfont /FontMatrix get dtransform E pop add newpath M
dup SW RL stroke grestore} D
/B {-10 3 R gsave CP newpath 2.5 0 360 arc closepath
UI 2 mod 0 eq {stroke} {fill} ie grestore 10 -3 R} D
/NP {/PM PN SN sub 2 mod D PN SN gt TU not PM 0 eq or and {showpage} if
0.6 setlinewidth LA /BP t D /PN PN 1 add D
/OF currentfont D RF findfont 10 scalefont setfont
/YO {LE neg 60 F div dup 40 gt {pop 40} if sub} D
NO {W 2 div YO M PN ST cvs S} if
ZL {0 YO M Zl S} if
ZR {W YO M Zr dup stringwidth pop neg 0 R S} if
OF setfont
XO SZ SL get neg M /CI 0 D} D
/SF {/CS E D CS SZ SL 3 2 roll put /YI CS -1.2 mul D dup ST cvs C1 E join
( NF ) join /C1 E D NF /BW ( ) SW pop D} D
/NF {FL E get findfont CS scalefont setfont} D
/FS {CF MK PF {1 or} if and or /CF E D FR SL CF put CF E SF} D
/FL [RF FF BF FB IF FI IB FT] D
/reencodeISO {
dup dup findfont dup length dict begin
{ 1 index /FID ne { def }{ pop pop } ie } forall
/Encoding ISOLatin1Encoding D
currentdict end definefont
} D
/ISOLatin1Encoding [
/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef
/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef
/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef
/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef
/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright
/parenleft/parenright/asterisk/plus/comma/minus/period/slash
/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon
/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N
/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright
/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m
/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde
/.notdef/backslash/parenleft/parenright/.notdef/.notdef/.notdef/.notdef
/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef
/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve
/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut
/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar
/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot
/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior
/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine
/guillemotright/onequarter/onehalf/threequarters/questiondown
/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla
/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex
/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis
/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute
/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis
/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave
/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex
/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis
/yacute/thorn/ydieresis
] D
FL {reencodeISO D} forall
/BS {/PC E D /TX E D /fin f D /CW 0 D /LK 0 D /SC 0 D
/RT TX D {RT ( ) search {/NW E D pop /RT E D /WH NW SW pop D CW WH add LL gt
{TX SC LK SC sub GI PC /SC LK D /CW WH BW add D}
{/CW CW WH add BW add D} ie
/LK LK NW length add 1 add D} {pop exit} ie} loop
/fin t D /LC TX length D /WH RT SW pop D CW WH add LL gt
{TX SC LK SC sub GI PC NL RT dup () ne {PC} {pop} ie}
{TX SC LC SC sub GI PC} ie} D
/BT {RS dup dup () ne E ( ) ne and
{/LS LL D /LL W L1 sub XO sub D {/CI 0 D SH /BP f D fin not {NL} if
/HM t D /LL LS D} BS} {BG {pop} {SH /BP f D} ie} ie} D
/BL {HM not {CP E pop XO E M} if} D
/NL {BL W XO sub L1 sub TB {BW add} if AT mul 2 div YA neg dup 0 lt {3 sub} if
R F0 CF ne {F0 NF} if C1 cvx exec /C1 () D /L1 0 D /F0 CF D
BP not {0 YB NN neg R} if /YA 0 D /YB 0 D CP /YC E D pop XO YC M YC LE neg gt
{YI CI sub dup 0 lt BP not and {dup 0 E R CI add /CI E D} {pop} ie}
{NP} ie /T t D} D
/RS {/TM E D /CN 0 D TM
{dup 9 eq E 10 eq or {TM CN ( ) PI} if
/CN CN 1 add D} forall /CN 0 D /BK HM EN and {0} {1} ie D TM
{dup 32 ne {TM CN 3 2 roll put /CN CN 1 add D /BK 0 D}
{pop BK 0 eq {TM CN 32 put /CN CN 1 add D} if /BK 1 D} ie}
forall TM 0 CN GI dup dup () ne E ( ) ne and
{dup CN 1 sub get 32 eq {/EN f D} {/EN t D} ie} if} D
/join {2 copy length E length add string dup 4 2 roll 2 index 0 3 index
PI E length E PI} bind D
/EG {dup 0 E dup () eq {E pop} {E get} ifelse 10 ne {(\\n) E join} if} D
/WR {PB {EG /PB f D} if PP {EG /PP f D} if (\\n) search
{dup () ne BP not or {TR /CI 0 D /NC 0 D NL pop WR}{pop pop WR} ie} {TR} ie} D
/TR {(\\t) search {dup length /NC E NC add D SH MS 0 8 NC 8 mod sub dup
/NC E NC add D GI SH pop TR} {dup length /NC E NC add D SH} ie
/BP f D} D
/SH {CP E pop LE neg lt {NP} if dup SW pop L1 add /L1 E D
C1 (\\() join T not {( ) join} if E join (\\)) join
AU AF and UF or {( U ) join} if ( S ) join /C1 E D /T t D /TB EN not D} D
/BG {CP pop XO sub abs 0.01 lt} D
/ON {AR AI NN get 1 add dup AR AI 3 -1 roll put ST cvs length dup ST E (. ) PI
ST 0 3 -1 roll 2 add GI dup SW pop neg 0 R S} D
/SP {YI E /YI E D NL /YI E D} D
/BR {BN} D
/BN {PF {WR} {BT NL} ie /HM f D} D
/AB {CH E 0 E put CH join WB} D
/NN {dup 0 lt {pop 0} if} D
/H {1 sub /HL E D E BN /AT E D CP E pop LE neg WW add lt {NP} if
BH SP /SL SL 1 add D HS HL get HT HL get FS} D
/EH {BN /AT 0 D AH SP /SL SL 1 sub NN D /CF 0 D SZ SL get FR SL get FS} D
/P {E PF {WR /PP t D} {BN AE not {AH SP} if} ie /AT E D} D
/HR {BN 0 CP E pop M gsave PF {0 YI R} if W 0 RL stroke grestore /CI 0 D
/BP f D NL} D
/AD {BN /AE t D AH SP 4 11 SF} D
/DA {BN /AE f D AH SP 0 11 SF} D
/PR {BN /AT 0 D /CF 0 D 9 PS mul 1 FS /PF t D /PB t D /NC 0 D} D
/PW {pop PR} D
/RP {PF {dup () ne {dup dup length 1 sub get 10 ne {(\\n) join} if} if
WR AH SP /CF 0 D 11 0 FS /PF f D} {BT} ie} D
/SI {/XO AI LG add NN IN mul BC NN BI mul add D /LL W XO sub D} D
/DT {BN /LG LG 1 sub D SI /LG LG 1 add D BL} D
/DD {BN SI BL} D
/DL {BN XO 0 eq {AH SP} if /LG LG 1 add D BL} D
/LD {BN LG 0 gt {/LG LG 1 sub D} if SI XO 0 eq {AH SP} if BL} D
/UL {BN XO 0 eq {AH SP} {NL} ie NR AI NN 0 put /UI UI 1 add D
/AI AI 1 add D SI BL} D
/LU {BN /UI UI 1 sub D /AI AI 1 sub D SI XO 0 eq {AH SP} {NL} ie BL} D
/OL {BN XO 0 eq {AH SP} {NL} ie NR AI NN 1 put /AI AI 1 add D SI BL} D
/LO {BN AR AI NN 0 put /AI AI 1 sub D SI XO 0 eq {AH SP} {NL} ie BL} D
/LI {BN /BP f D /CI 0 D CP E pop LE neg lt {NP} if
/C1 C1 NR AI 1 sub NN get 1 eq {( ON )} {( B )} ie join D BL} D
/BQ {BN AH SP /BC BC 1 add D SI BL} D
/QB {BN AH SP /BC BC 1 sub D SI BL} D
/WB {PF {WR} {BT} ie} D
/A {WB /AF t D} D
/EA {WB /AF f D} D
/SS {SZ SL get /SL SL 1 add D} D
/I {WB SS 4 FS} D
/BD {WB SS 2 FS} D
/TT {WB SS 1 FS} D
/KB {WB SS /CF 0 D 3 FS} D
/CT {WB SS /CF 0 D 4 FS} D
/SM {WB SS /CF 0 D 1 FS} D
/ES {WB /SL SL 1 sub NN D /CF 0 D SZ SL get FR SL get FS} D
/UN {WB /UF t D} D
/NU {WB /UF f D} D
/SB {0 1 NI {/N E D /K WS N get D /NY AY N get FC N get mul D /BV NY array D
0 1 NY 1 sub {/TM K string D currentfile TM readhexstring pop pop BV E TM put}
for BM N BV put} for} D
/colorimage where {pop} {
/colorimage {
pop pop /Bp E D /Gp E D /Rp E D
{/Re Rp D /Gr Gp D /Bl Bp D
0 1 Re length 1 sub {
/i E D Gr i Re i get 0.299 mul Gr i get 0.587 mul add Bl i get 0.114 mul add
cvi put
} for Gr} image
} D
} ie
/IP {BV N get /N N 1 add D} D
/II {/K E D /TY E D /XW AX K get D /YW AY K get D /IS SG IT K get get D
/XS XW IS mul D /YS YW IS mul D YS CS sub TY 2 eq {/MB E D /MA 0 D}
{TY 2 mod 1 eq {2 div /MA E 2 sub D /MB MA 4 add D}{/MA E D /MB 0 D} ie} ie} D
/IM {II /ty TY D /xs XS D /ys YS D /ya YA D /yb YB D /ma MA D /mb MB D /k K D
/CI 0 D WB L1 xs add dup /L1 E D XO add W gt
{NL /YA ma D /YB mb D /YS ys D /L1 xs D}
{ma YA gt {/YA ma D} if mb YB gt {/YB mb D} if} ie CP E pop YS sub LE neg lt
{NP /YA ma D /YB mb D /L1 xs D} if /BP f D ty ST cvs ( ) join k ST cvs join
C1 E join ( DI ) join FP 2 eq FP 1 eq AF and or {( FM ) join} if /C1 E D
/HM t D /EN f D /T TY 3 eq D} D
/DI {II /YN YW neg D /HM t D /CI 0 D /K2 IX K get D gsave CP MB sub translate
K2 0 ge {/DP AZ K2 get D /BV BM K2 get D XS YS scale /N 0 D XW YW DP
[XW 0 0 YN 0 YW] {IP} FC K2 get 1 eq {image} {{IP} {IP} t 3 colorimage} ie}
{PV K2 neg 1 sub get exec} ie grestore XS 0 R} D
/FM {gsave CP MB sub translate XS neg 0 M 0 YS RL XS 0 RL 0 YS neg RL XS neg
0 RL stroke grestore} D
/BH -35 D
/AH -28 D
/LL W D
/XO 0 D
/YI 0 D
/CI 0 D
/LG 0 D
/AI 0 D
/UI 0 D
/IN 30 D
/BI 12 D
/WW 50 D
/AR [0 0 0 0 0 0 0 0 0 0] D
/NR [0 0 0 0 0 0 0 0 0 0] D
/FR [0 0 0 0 0 0 0 0 0 0] D
/SZ [0 0 0 0 0 0 0 0 0 0] D
/SL 0 D
/CF 0 D
/BC 0 D
/YA 0 D
/YB 0 D
/F0 0 D
/N 0 D
/AT 0 D
/C1 () D
/C2 () D
/L1 0 D
/L2 0 D
/PN SN D
/MS ( ) D
/CH 1 string D
/ST 6 string D
/HM f D
/PF f D
/EN f D
/TB f D
/UF f D
/PP f D
/AE f D
/AF f D
/BP t D
/T t D
EOT
$nimg=-1;
$nbm=-1;
s|\r||g;
s|\\|\\200|g;
s|\(|\\201|g;
s|\)|\\202|g;
&openps if ($opt_o);
# Yes, I know Perl has case-insensitive pattern matching. But on my system
# it takes about 10 times longer to run!
if(/<[pP][lL][aA][iI][nN][tT][eE][xX][tT] *>/) {$_=$`;$pt=$'};
while($_){
if(/(<[lL][iI][sS][tT][iI][nN][gG] *>)/) {$_=$`; $tag=$1; $rest=$';
if(/<[xX][mM][pP] *>/) {$_=$`; &Subst; $P2.=")XX("; $_=$'.$tag.$rest;
if(m|</[xX][mM][pP] *>|) {$P2.="$`)RP("; $_=$'}
else {$P2.=$'; $_=""}}
else {&Subst; $P2.=")XX("; $_=$rest;
if(m|</[lL][iI][sS][tT][iI][nN][gG] *>|) {$P2.="$`)RP("; $_=$'}
else {$P2.=$'; $_=""}}}
elsif(/<[xX][mM][pP] *>/) {$_=$`; &Subst; $P2.=")XX("; $_=$';
if(m|</[xX][mM][pP] *>|) {$P2.="$`)RP("; $_=$'}
else {$P2.=$'; $_=""}}
else {&Subst;$_=""}
}
if($pt) {$P2.=")XX($pt"};
$P2.=")";
while($P2=~/XX\(([^)]*)\)/) {
$beg=$`;
$mat=$1;
$end=$';
$mat=~s/(.*\n){30}.*/$&)WR(/g;
$P2=$beg."PR(".$mat.")".$end;
}
print $P1;
if($nimg>=0) {
print "/AX [".join(' ',@XS)."] D\n";
print "/AY [".join(' ',@YS)."] D\n";
print "/IX [".join(' ',@IX)."] D\n";
print "/IT [".join(' ',@IT)."] D\n";
if($nbm>=0) {
print "/AZ [".join(' ',@DP)."] D\n";
print "/WS [".join(' ',@WS)."] D\n";
print "/FC [".join(' ',@FC)."] D\n";
print "/NI $nbm D\n";
print "/BM ",$nbm+1," array D\n%%EndProlog\nSB\n";
for $i (0..$nbm) {print "$BM[$i]\n"}
}
if($nps) {
print "/PV [".join("\n",@PS)."] D\n";
}
}
print <<EOP;
0 11 SF
0 LE neg 20 sub M
(
EOP
print $P2."WB NL showpage end restore\n";
unlink <$scr*>;
exit;
sub Subst{
s|<!--NewPage-->|)WB NL NP(|g;
while (/<!--/) {
$beg=$`;
if(/--[ \t\n]*>/) {
$_=$beg.$';
} else {
$_=$beg;
}
}
s|</[pP][rR][eE] *>[ \n]*<[pP][rR][eE]>||g;
s|</?[pP]>[ \n]*<[pP][rR][eE]>|)XX(|g;
s|<[lL][iI]>[ \n]*<[pP]>|)0 P()LI(|g;
s|<[hH]([1-6])[ \n]+[aA][lL][iI][gG][nN] *= *['"]?[cC][eE][nN][tT][eE][rR][^>]*>|)1 $1 H(|g;
s|<[hH]([1-6])[ \n]+[aA][lL][iI][gG][nN] *= *['"]?[rR][iI][gG][hH][tT][^>]*>|)2 $1 H(|g;
s|<[hH]([1-6]) *[^>]*>|)0 $1 H(|g;
s|</[hH][1-6]>|)EH(|g;
s|<[bB][rR][^>]*>|)BR(|g;
s|<[pP]>|)0 P(|g;
s|<[pP][ \n]+[aA][lL][iI][gG][nN] *= *['"]?[cC][eE][nN][tT][eE][rR][^>]*>|)1 P(|g;
s|<[pP][ \n]+[aA][lL][iI][gG][nN] *= *['"]?[rR][iI][gG][hH][tT][^>]*>|)2 P(|g;
s|<[pP][ \n]+[^>]*>|)0 P(|g;
s|</[pP]>|)0 P(|g;
s|<[hH][rR][^>]*>|)HR(|g;
s|<[aA][dD][dD][rR][eE][sS][sS] *>|)AD(|g;
s|</[aA][dD][dD][rR][eE][sS][sS]>|)DA(|g;
s|<[pP][rR][eE] *>|)XX(|g;
s|<[pP][rR][eE] *[wW][iI][dD][tT][hH] *= *['"]* *(\d+) *['"]* *>|)$1 PW(|g;
s|</[pP][rR][eE]>|)RP(|g;
s|<[dD][tT] *>|)DT(|g;
s|<[dD][dD] *>|)DD(|g;
s|<[dD][lL][^>]*>|)DL(|g;
s|</[dD][lL]>|)LD(|g;
s|<[uU][lL][^>]*>|)UL(|g;
s|</[uU][lL]>|)LU(|g;
s|<[mM][eE][nN][uU] *>|)UL(|g;
s|</[mM][eE][nN][uU]>|)LU(|g;
s|<[dD][iI][rR] *>|)UL(|g;
s|</[dD][iI][rR]>|)LU(|g;
s|<[oO][lL] *[^>]*>|)OL(|g;
s|</[oO][lL]>|)LO(|g;
s|<[lL][iI] *>|)LI(|g;
s|</[lL][iI] *>||g;
s|<[bB][lL][oO][cC][kK][qQ][uU][oO][tT][eE] *>|)BQ(|g;
s|</[bB][lL][oO][cC][kK][qQ][uU][oO][tT][eE]>|)QB(|g;
s|<[sS][tT][rR][oO][nN][gG] *>|)BD(|g;
s|</[sS][tT][rR][oO][nN][gG]>|)ES(|g;
s|<[sS][aA][mM][pP] *>|)SM(|g;
s|</[sS][aA][mM][pP]>|)ES(|g;
s|<[cC][iI][tT][eE] *>|)CT(|g;
s|</[cC][iI][tT][eE]>|)ES(|g;
s|<[vV][aA][rR] *>|)I(|g;
s|</[vV][aA][rR]>|)ES(|g;
s|<[bB] *>|)BD(|g;
s|</[bB]>|)ES(|g;
s|<[iI] *>|)I(|g;
s|</[iI]>|)ES(|g;
s|<[tT][tT] *>|)TT(|g;
s|</[tT][tT]>|)ES(|g;
s|<[uU] *>|)UN(|g;
s|</[uU]>|)NU(|g;
s|<[dD][fF][nN] *>|)I(|g;
s|</[dD][fF][nN]>|)ES(|g;
s|<[eE][mM] *>|)I(|g;
s|</[eE][mM]>|)ES(|g;
s|<[cC][oO][dD][eE] *>|)SM(|g;
s|</[cC][oO][dD][eE]>|)ES(|g;
s|<[kK][bB][dD] *>|)KB(|g;
s|</[kK][bB][dD]>|)ES(|g;
s|<[aA][ \n]+[^>]*[hH][rR][eE][fF][^>]*>|)A(|g;
s|</[aA]>|)EA(|g;
for $char (keys %latin1) {s|&$char;?|\\$latin1{$char}|g}
while (/<([iI][mM][gG]|[fF][iI][gG])[ \n]/) {
$beg=$`;
$'=~/>/;
$img=$`;
$end=$';
$img=~s/\n/ /g;
if($opt_T) {
&getalt
} else {
$al=0;
($align)=$img=~/align *= *['"]?(\w*)/i;
if($align=~/^middle$/i) {$al=1};
if($align=~/^top$/i) {$al=2};
($url)=$img=~/src *= *"([^"]*)"/i;
unless ($url) {($url)=$img=~m|src *= *([\w\.\-:/~%]+)|i};
($url)=$url=~/([^ \n]*)/;
print STDERR "\nImage: $url\n" if($opt_X);
$URL=$url;
$text=$cmd{$url};
unless ($text) {
if(!$url || $failed{$url}) {
&getalt
} else {
unless($url=~m|://|) {
if($url=~m|^http:(.*)|) {$url=$1}
if($url=~m|^/|) {$URL=$b1.$url} else {$URL=$b2.$url}
}
print STDERR "Expanded to: $URL\n" if($opt_X);
&pictops;
if($bm || $ps) {
print STDERR "Size: ".$xs."x".$ys."\n" if($opt_X);
$nimg++;
push (@XS,$xs);
push (@YS,$ys);
if($bm) {
$nbm++;
push (@DP,$dp);
push (@BM,$bm);
push (@WS,int(($xs-1)*$dp/8)+1);
push (@FC,$fc);
push (@IX,$nbm);
push (@IT,0);
}
if($ps) {
$nps--;
push (@IX,$nps);
push (@PS,$ps);
push (@IT,1);
}
$text=" $nimg IM(";
$cmd{"$url"}=$text;
} else {
&getalt;
$failed{"$url"}=1;
}
}
}
}
if($cmd{$url}) {$text=")".$al.$text}
$_=$beg.$text.$end
}
if(/<[mM][aA][tT][hH]/) {chdir $tmpdir || die "Cannot cd to $tmpdir\n"};
while (/<[mM][aA][tT][hH]/) {
$beg=$`;
$rest=$&.$';
$rest=~m|</[mM][aA][tT][hH]>|;
$end=$';
$math=$`;
$math=~s|\\200|\\|g;
$math=~s|\\201|\(|g;
$math=~s|\\202|\)|g;
&math2tex($math);
open(SCRATCH,">$scr.tex");
print SCRATCH $tex;
close SCRATCH;
unlink $scr.ps;
`echo r|tex $scr.tex;dvips -E $scr.dvi`;
open(PS,"$scr.ps");
$pic=<PS>;
if($pic=~/^%!/ && $pic=~/%%BoundingBox: +($F) +($F) +($F) +($F)/) {
$xs=$3-$1;
$ys=$4-$2;
$ps="{save\n0 0 M 1 F div dup scale /showpage {} D -$1 -$2 translate\n";
for $i (split(/\n/,$pic)) {
$ps.=$i."\n" if($i && $i!~/^%/)
}
}
$ps.="restore}\n";
$nimg++;
$nps--;
push (@XS,$xs);
push (@YS,$ys);
push (@IX,$nps);
push (@PS,$ps);
push (@IT,2);
$_=$beg.")3 $nimg IM(".$end;
}
s|<[^ >][^>]*>||g;
s|<?|<|g;
s|>?|>|g;
s|"?|"|g;
s|&#(\d+);?|)$1 AB(|g;
s|&?|\&|g;
$P2.=$_
}
sub math2tex {
local($_)=@_;
local($beg,$rest);
%a=("line","overline",
"cub","overbrace",
"hat","widehat",
"tilde","widetilde",
"larr", "overleftarrow",
"rarr", "overrightarrow");
%b=("line","underline",
"cub","underbrace",
"hat","widehat",
"tilde","widetilde");
%s=("medium","\\big",
"large","\\Big",
"huge","\\bigg");
$st='\rm' if(/<math[ \n\t]+class[ \n\t]*=[ \n\t]*["']?chem["']?[ \n\t]*>/i);
s/<math[^>]*>//gi;
s/__/_\\>_/gi;
s/\^\^/^\\>^/gi;
s/_([^_]+)_/_{$1}/g;
s/\^([^^]+)\^/^{$1}/g;
s/ ?/\\,/gi;
s/&sp;?/\\>/gi;
s/ ?/\\;/gi;
s/\\240/\\>/gi;
s/&vepsilon;?/\\varepsilon /gi;
s/&vtheta;?/\\vartheta /gi;
s/ο?/o/gi;
s/&vsigma;?/\\varsigma /gi;
s/&inf;?/\\infty /gi;
s/∫?/\\int\\limits /gi;
s/∑?/\\sum\\limits /gi;
s/∏?/\\prod\\limits /gi;
s/→?([_^])/\\mathop\\rightarrow\\limits$1 /gi;
s/→?/\\rightarrow /gi;
s/&pd;?/\\partial /gi;
s/<t>/\\hbox{/gi;
s/<b>/\\bf /gi;
s/<bt>/\\bf\\hbox{/gi;
s/<sub[^>]*>/_{/gi;
s/<sup[^>]*>/\^{/gi;
s/<box[ \n\t]*size=["']?(\w+)["']?>/{\\def\\lft{$s{$1}}\\def\\rgt{$s{$1}}/gi;
s/<box[^>]*>/{/gi;
s/<text[ \t\n]*>/\\hbox{/gi;
s/([\(\[\|]) *<left>/\\lft$1/gi;
s/<right>/\\rgt /gi;
s/<(atop|choose|over)>/\\\L$1 /gi;
s/<of>/}\\of{/gi;
s/<bar>/\\overline{/gi;
s/<vec>/\\overrightarrow{/gi;
s/<hat>/\\widehat{/gi;
s/<tilde>/\\widetilde{/gi;
s/<(sqrt|root|vec|dot|ddot|hat|tilde)>/\\\L$1\{/gi;
while(/<above[ \t\n]+sym[ \t\n]*=[ \t\n]*["']?equals["']?[ \t\n]*>/i) {
$beg=$`."\\overline{\\overline{";
$rest=$';
$rest=~s/<\/above>/}}/i;
$_=$beg.$rest;
}
s/<above[ \t\n]*>/\\overline{/gi;
s/<above[ \t\n]+sym[ \t\n]*=[ \t\n]*["']?(\w+)["']?[ \t\n]*>/\\$a{$1}\{/gi;
s/<below[ \t\n]*>/\\underline{/gi;
s/<below[ \t\n]+sym[ \t\n]*=[ \t\n]*["']?(\w+)["']?[ \t\n]*>/\\$b{$1}\{/gi;
s/<\/(math|row|item|b)>//gi;
s/<\/(box|t|bt|sup|sub|sqrt|root|vec|bar|dot|ddot|hat|tilde|above|below|text|array)>/}/gi;
s/<?/< /gi;
s/>?/>/gi;
s/&(\w+);?/\\$1 /gi;
s/<array[^>]*>/\\matrix{/gi;
s/<row>[ \t\n]*<item[^>]*>//i;
s/<row>[ \t\n]*<item[^>]*>/\\cr /gi;
s/<item>/&/gi;
s/<[^ ][^>]*>//gi;
s/\n*$//;
$tex="\\magnification=$mag\\nopagenumbers\n\\def\\lft{\\left}"
."\\def\\rgt{\\right}\n\$\$$st".$_."\$\$\n\\end\n";
}
sub Getopts {
local($optlist)=@_;
local(@args,$_,$opt,$opts,$rest,$olist,$plist,$found,@popts);
local($errs)=0;
local($[)=0;
@args=split( /\|/, $optlist );
for $opt (@args) {
if(substr($opt,-1,1) ne ':') {$olist.=$opt}
else {$plist.=$opt}
}
@popts=split(/:/,$plist);
while(@ARGV && ($_=$ARGV[0]) =~ /^-(.*)/) {
$opt=$1;
if($opt =~ /^[$olist]+$/) {
while ($char=chop $opt) {eval "\$opt_$char=1"}
shift(@ARGV);
}
else {
$found=0;
for $opts (@popts) {
$rest=substr($opt,length($opts));
if(index($opt,$opts)==0) {
$found=1;
shift(@ARGV);
if(length($rest)==0) {
++$errs unless @ARGV;
$rest=shift(@ARGV)
}
eval "\$opt_$opts=\$rest";
eval "\$set_$opts=1"
}
}
if(!$found) {
print STDERR "Unknown option: $opt\n";
++$errs;
shift(@ARGV)
}
}
}
$errs==0
}
sub openps {
open(STDOUT,">$opt_o") || die "Error opening $opt_o for output\n"
}
sub getalt {
$alt="";
if($img=~/alt *= *"([^"]*)"/i) {$alt=$1; $match=1}
if(!$match && $img=~/alt *= *([\w\.-]+)/i) {$alt=$1; $match=1}
unless($match) {$alt=$opt_IM}
$text=" $alt )WB("
}
sub xbmtops {
$fc=1;
$dp=1;
($xs,$ys)=$pic=~/^#define.* (\d+)[\w\W\n]*^#define.* (\d+)/;
$nd=2*int(($xs+7)/8)*$ys;
($pic)=$pic=~/[^#].* char.*[\w\W\n]*{([\w\W\n]*)}/;
$pic=~s/[ ,\n]*0x[ ,]*//g;
$pic=~y/01246789bdef/f7bd91e62480/;
$bm=unpack("H*", pack("h*",$pic));
}
sub pmtops {
$pm=~/^P([4-6])/;
$maptype=$1;
return if(!$maptype);
$pm=$';
$bm="";
$wh="[ \t\r\n]";
$nint=3;
$dp=8;
if($maptype==4) {
$nint=2;
$dp=1;
}
undef @num;
$found=0;
while($pm && $found<$nint) {
if($pm=~/^$wh*(\d+)/) {$num[$found]=$1};
if($num[$found]) {
$found++;
$pm=$';
} elsif($pm=~/^$wh*#.*\n/) {
$pm=$';
} else {
return;
}
}
($b)=$pm=~/$wh([\w\W\n]*)/;
($xs,$ys,$bits)=@num;
return if($bits>255);
$fc=1;
if($maptype==6) {
$fc=3;
$nd=6*$xs*$ys;
for $j (0..$ys-1) {
$tmp="";
@w=split(//,substr($b,3*$xs*$j,3*$xs));
for $k (0..2) {
for $i (0..$xs-1) {
$tmp.=$w[3*$i+$k];
}
}
$bm.=$tmp;
}
$bm=unpack("H*",$bm);
} else {
$bm=unpack("H*",$b);
if($maptype==4) {
$nd=2*int(($xs+7)/8)*$ys;
$bm=~y/0123456789abcdef/fedcba9876543210/;
} else {
$nd=2*$xs*$ys;
}
}
}
sub trans {
$next = 13;
$temp = ord substr($pic,10,1);
if($temp & 0x80) {$next += 3*2**(($temp & 0x07) + 1)} else {return};
$byte = ord substr($pic,$next,1);
while($byte != 0x3b && $next <= length $pic) {
if($byte == 0x21) {
if(ord substr($pic,$next+1,1) == 0xf9) {
if(ord substr($pic,$next+3,1) & 0x01) {
print STDERR "Transparent\n" if($opt_X);
$index = ord substr($pic,$next+6,1);
substr($pic,3*$index+13,3) = "\377\377\377";
}
return;
$next += 2;
&skip;
} else {
$next += 2;
&skip;
}
} elsif($byte == 0x2c) {
$next += 10;
$temp = ord substr($pic,$next-1,1);
if($temp & 0x80) {$next += 3*2**(($temp & 0x07) + 1)};
$next++;
&skip;
} else {return}
}
}
sub skip {
$byte = ord substr($pic,$next,1);
while($byte != 0) {
$next += $byte + 1;
$byte = ord substr($pic,$next,1);
}
$next++;
$byte = ord substr($pic,$next,1);
}
sub run {
print STDERR "@_\n" if($opt_X);
$pm=`@_`;
}
sub gethtml {
$_=`$geturl '$html'`;
}
sub pictops {
if($opt_g) {
$fc=1;
$pg1="pgm";
$pg2="|ppmtopgm"
} else {
$fc=3;
$pg1="ppm";
$pg2=""
}
($type)=$URL=~/([^\?]+)\??/;
($type)=$type=~/\.(\w+)$/;
$bm="";
$ps="";
$pm="";
if($URL=~m|://|) {
$pic=`$geturl '$URL'`;
} else {
$flag=0;
if($opt_O) {
$orig=$URL;
unless($orig=~s/\.\w*$/.ps/) {$orig.=".ps"};
if(open(ORIG,"$orig")) {
$pic=<ORIG>;
close ORIG;
if($pic=~/^%!/ && $pic=~/%%BoundingBox:/) {
$flag=1;
print STDERR "Using $orig as original for $URL\n" if($opt_X);
}
}
}
if(!$flag) {
open(PIC,"$URL");
$pic=<PIC>;
close PIC;
}
}
$pic=~s/^[\n\r]*//;
&trans if($pic=~/^GIF/);
open(SCRATCH,">$scr");
print SCRATCH $pic;
close SCRATCH;
if($pic=~/^%!/ && $pic=~/%%BoundingBox: +($F) +($F) +($F) +($F)/) {
$xs=$3-$1;
$ys=$4-$2;
$ps="{save\n0 0 M\nIS IS scale\n/showpage {} D\n-$1 -$2 translate\n";
for $i (split(/\n/,$pic)) {
$ps.=$i."\n" if($i && $i!~/^%/)
}
$ps.="restore}\n";
} elsif($type=~/.xbm$/i || $pic=~/^#define/) {
&xbmtops;
} elsif($have_ImageMagick) {
&run("convert $scr $pg1:-");
if(!$pm && $pic=~/^\377\330/ && $have_djpeg) {
&run("djpeg $scr$pg2");
}
} elsif($pic=~/^\377\330/ && $have_djpeg) {
&run("djpeg $scr$pg2");
} elsif($have_pbmplus || $have_netpbm) {
if($pic=~/^GIF/) {
&run("$giftopm $scr");
} else {
&run("anytopnm $scr");
}
if($opt_g && $pm=~/^P6/) {
open(SCRATCH,">$scr");
print SCRATCH $pm;
close SCRATCH;
&run("ppmtopgm $scr");
}
} elsif($have_sgi) {
`$giftopm $pic /tmp/__xXx.rgb 2>/dev/null`;
`toppm /tmp/__xXx.rgb /tmp/__xXx.ppm 2>/dev/null`;
`/bin/rm -f /tmp/__xXx.rgb /tmp/__xXx.map $pic`;
&run("cat /tmp/__xXx.ppm 2>/dev/null");
}
&pmtops if(!$bm);
if(!$bm) {return};
$bm=substr($bm,0,$nd);
$pad=$nd-length($bm);
if($pad) {$bm.="f" x $pad};
$bm=~s/(.{60})/$1\n/g;
if($have_sgi) {
`/bin/rm -f /tmp/__xXx.ppm`;
}
}